package com.syni.mdd.sjb.common.datatreaure.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DxshAppMinappAuth;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.mapper.DxshAppMinappAuthMapper;
import com.syni.mdd.sjb.common.component.utils.common.utils.BeanUtils;
import com.syni.mdd.sjb.common.component.utils.common.utils.RpcResponseVo;
import com.syni.mdd.sjb.common.component.utils.feign.dto.mddAll.apiBusiness.CertificationBusinessDto;
import com.syni.mdd.sjb.common.component.utils.feign.service.mddAll.BusinessRestService;
import com.syni.mdd.sjb.common.datatreaure.service.DxshAppMinappAuthService;
import com.syni.mdd.sjb.common.datatreaure.constants.Constants;
import com.syni.mdd.sjb.common.datatreaure.constants.ResultMsg;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 小程序资质信息表 服务实现类
 * </p>
 *
 * @author lcc
 * @since 2020-05-18
 */
@Service
public class DxshAppMinappAuthServiceImpl extends ServiceImpl<DxshAppMinappAuthMapper, DxshAppMinappAuth> implements DxshAppMinappAuthService {

    @Autowired
    BusinessRestService businessRestService;

    @Autowired
    DxshAppMinappAuthMapper dxshAppMinappAuthMapper;

    @Override
    public RpcResponseVo certificationBusiness(CertificationBusinessDto certificationBusinessDto, DxshAppMinappAuth dxshAppMinappAuth) {


            BeanUtils.copyProperties(certificationBusinessDto,dxshAppMinappAuth);
            this.saveOrUpdate(dxshAppMinappAuth);
            certificationBusinessDto.setBusinessAuthId(dxshAppMinappAuth.getId());

            if(BeanUtils.checkPropertiesNull(dxshAppMinappAuth,
                    "licenseType","licenseNo","licenseName",
                    "licenseImg","identityType","identityName","identityNo"
                    ,"identityPoImg","identityBaImg")){
                certificationBusinessDto.setIsFinished(Constants.COMMON_FLAG_TRUE_INT);

                if(dxshAppMinappAuth.getIsIdentityExpired() == Constants.COMMON_FLAG_FALSE_INT &&
                        ( dxshAppMinappAuth.getIdentityStartTime() == null ||
                                dxshAppMinappAuth.getIdentityExpired() == null) ){
                    //如果选择了指定日期的身份证
                    certificationBusinessDto.setIsFinished(Constants.COMMON_FLAG_FALSE_INT);
                }
                if(dxshAppMinappAuth.getIsExpired() == Constants.COMMON_FLAG_FALSE_INT &&
                        ( dxshAppMinappAuth.getLicenseStartTime() == null ||
                                dxshAppMinappAuth.getLicenseExpired() == null) ){
                    //如果选择了指定日期的营业执照
                    certificationBusinessDto.setIsFinished(Constants.COMMON_FLAG_FALSE_INT);
                }
            }else {
                certificationBusinessDto.setIsFinished(Constants.COMMON_FLAG_FALSE_INT);
            }
            return RpcResponseVo.success(certificationBusinessDto, ResultMsg.HAND_SUCCESS);

    }

    @Override
    public DxshAppMinappAuth createAuth(Integer userId,Integer businessId,Integer channelId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DxshAppMinappAuth.USER_ID,userId);
        queryWrapper.eq(DxshAppMinappAuth.BUSINESS_ID,businessId);
        queryWrapper.eq(DxshAppMinappAuth.CHANNEL_ID,channelId);
        DxshAppMinappAuth dxshAppMinappAuth = this.getOne(queryWrapper);
        if(dxshAppMinappAuth == null){
            dxshAppMinappAuth = new DxshAppMinappAuth();
            dxshAppMinappAuth.setUserId(userId);
            dxshAppMinappAuth.setBusinessId(businessId);
            dxshAppMinappAuth.setChannelId(channelId);
            dxshAppMinappAuth.setNewTime(LocalDateTime.now());
            this.save(dxshAppMinappAuth);
            return dxshAppMinappAuth;
        }else{
            return dxshAppMinappAuth;
        }

    }

    @Override
    public void deleteChannelData(Integer id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DxshAppMinappAuth.CHANNEL_ID,id);
        dxshAppMinappAuthMapper.delete(queryWrapper);
    }

    @Override
    public DxshAppMinappAuth getListByChannelIdAndUserId(Integer channelId, Integer userId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DxshAppMinappAuth.CHANNEL_ID,channelId);
        queryWrapper.eq(DxshAppMinappAuth.USER_ID,userId);
        List<DxshAppMinappAuth> dxshAppMinappAuthList = dxshAppMinappAuthMapper.selectList(queryWrapper);
        if(CollectionUtils.isNotEmpty(dxshAppMinappAuthList)){
            return dxshAppMinappAuthList.get(0);
        }
        return null;
    }

    @Override
    public DxshAppMinappAuth getMinAppAuthByChannelId(Integer channedId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DxshAppMinappAuth.CHANNEL_ID,channedId);
        List<DxshAppMinappAuth> dxshAppMinappAuths = dxshAppMinappAuthMapper.selectList(queryWrapper);
        if(CollectionUtils.isNotEmpty(dxshAppMinappAuths)){
            return dxshAppMinappAuths.get(0);
        }
        return null;
    }
}
